<template>
  <div>
    <div class="queryForm">
      <el-input size="large" v-model="queryParams.meansName" placeholder="请输入关键字" style="width: 500px;">
        <template #append><el-button class="btn" @click="handleQuery">搜索</el-button></template>
      </el-input>
    </div>
    <p class="tag">
      <span style="width: 100px;">发布日期：</span>
      <el-space wrap :size="20" style="flex: 1;">
        <template
          v-for="item, index in [{ label: '全部', value: '' }, { label: '近三年', value: '3 year' }, { label: '近一年', value: '1 year' }, { label: '近一月', value: '1 month' }]"
          :key="index">
          <span @click="chickHandle(item, index)" :class="[{ active: index === current }, 'spanStyles']">{{ item.label
          }}</span>
        </template>
      </el-space>
    </p>
    <p class="tag">
      <span style="width: 100px;">案例类别：</span>
      <el-space wrap :size="20" style="flex: 1;">
        <template v-for="item, index in option" :key="index">
          <span @click="chickHandle1(item, index)" :class="[{ active: index === current1 }, 'spanStyles']">{{
            item.typeName
          }}</span>
        </template>
      </el-space>
    </p>

    <el-divider></el-divider>
    <el-table v-loading="loading" ref="tableRef" :data="dataList" stripe row-key="id" style="margin-bottom: 20px;">
      <el-table-column label="序号" align="center" width="60">
        <template #default="scope">
          <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="案例名称" align="center" prop="meansName" />
      <el-table-column label="发布日期" align="center" prop="releaseDate" />
      <el-table-column label="类别" align="center" prop="meansType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text">
              <router-link target="_blank"
              :to="{ path: '/viewItemFile', query: { id: scope.row.meansUpload } }">预览</router-link>
              <!-- :to="{ path: '/viewItemFile', query: { id: scope.row.meansUploadAtt[0].attachId } }">预览</router-link> -->
            </el-button>
          <el-button type="text" @click="handleDown(scope.row)" v-if="scope.row.isPermitDownload=='0'">下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
      @pagination="getList" />
  </div>
</template>
  
<script setup name="typicalCase">
import { getPurpose, getAllList } from "@/api/serviceCase/typicalCase.js"
import { onMounted } from "vue";
import {useWebsocket} from "@/utils/socket"

const option = ref([]);
const dataList = ref([]);
const loading = ref(false);
const total = ref(0);
const data = reactive({
  queryParams: {
    page: 1,
    rows: 10,
    meansName: undefined,
    releaseDate: '',
    meansTypeId: undefined,
    releaseColumn: '典型案例',
    releaseStatus:0,
  },
})
const { queryParams, } = toRefs(data);
const current = ref(0);
const current1 = ref(0);

// 下载文件
function handleDown(item) {
  var query = {
    attachId: item.meansUpload,
  }
  location.href = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(query.attachId);
}

function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

function chickHandle(item, index) {
  current.value = index;
  queryParams.value.releaseDate = item.value;
  handleQuery();
}

function chickHandle1(item, index) {
  if(index==current1.value)return;
  current1.value = index;
  if (item.id == 0) {
    queryParams.value.meansTypeId = '';
  } else {
    queryParams.value.meansTypeId = item.id;
  }
  handleQuery();
}

function getType() {
  getPurpose({ plate: '典型案例' }).then(res => {
    if (res.data) {
      option.value = [{ typeName: '全部', id: 0, }, ...res.data]
    } else {
      option.value = []
    }
  })
}

/** 查询数据列表 */
function getList() {
  loading.value = true;
  getAllList({ ...queryParams.value }).then(response => {
    console.log(response.data);
    dataList.value = response.data.rows ? response.data.rows : [];
    total.value = response.data.total;
    loading.value = false;
  });
}

// WebSocket
const ws = useWebsocket()
ws.onmessage = (e) => {
    if(JSON.parse(e.data).moduleType=='serviceCase'){
      getAllList({ ...queryParams.value }).then(response => {
        dataList.value = response.data.rows ? response.data.rows : [];
        total.value = response.data.total;
      });
    }else if(JSON.parse(e.data).moduleType=='typeManage'){
      getType();
    }
  };

//页面卸载，关闭socket
onUnmounted(() => {
  ws.close();
});

onMounted(() => {
  getType();
  getList();
})
</script>
  
<style scoped lang="scss">
.queryForm {
  padding: 20px 30px;
  width: 100%;
  text-align: center;
  margin: 10px 0px;

  .el-input-group__append {
    background-color: #107ce5 !important;
    color: #fff !important;
  }

  .btn {
    height: 40px;
    background-color: #107ce5 !important;
    color: #fff !important;
    border-radius: 0px !important;
  }
}

.tag {
  display: flex;
  justify-content: space-between;
  color: #333333;
  font-size: 16px;

  span {
    height: 30px;
    line-height: 30px;
    display: inline-block;
  }

  .spanStyles {
    display: inline-block;
    padding: 0px 10px;
    height: 30px;
    line-height: 30px;
    border-radius: 4px;
    cursor: pointer;
  }

  .active {
    color: #107ce5;
    background-color: #c6e2ff;
    cursor: pointer;
  }
}
</style>
  
  